﻿using FastMember;
using fuzhu;
using LH.Model.ST;
using log4net.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices.Protocols;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml.XPath;
using XXX.Common;
using XXX.Models;

namespace XXX.Bo
{
   public class XmlOperation
    {

        //获取xml头
        public static string GetCOXmlHead<T>(T rdhead)
        {
            XNamespace rsNs = "urn:schemas-microsoft-com:rowset";
            XNamespace zNs = "#RowsetSchema";

            var rootHead = new XElement(rsNs + "data", new XAttribute(XNamespace.Xmlns + "rs", rsNs));
            var rowHead = new XElement(zNs + "row", new XAttribute(XNamespace.Xmlns + "z", zNs));

            var accessor = TypeAccessor.Create(rdhead.GetType());

            foreach (var member in accessor.GetMembers())
            {
                var value = accessor[rdhead, member.Name];
                if (value != null)
                {
                    rowHead.Add(new XAttribute(member.Name, value));
                }
            }

            rootHead.Add(rowHead);
            return rootHead.ToString();
        }
        //获取xml表体
        public static string GetCOXmlBody<T>(List<T> rdbodies)
        {
            XNamespace rsNs = "urn:schemas-microsoft-com:rowset";
            XNamespace zNs = "#RowsetSchema";

            var rootBody = new XElement(rsNs + "data", new XAttribute(XNamespace.Xmlns + "rs", rsNs));

            foreach (var bodyItem in rdbodies)
            {
                var rowBody = new XElement(zNs + "row", new XAttribute(XNamespace.Xmlns + "z", zNs));

                var accessorbody = TypeAccessor.Create(bodyItem.GetType());

                foreach (var member in accessorbody.GetMembers())
                {
                    var value = accessorbody[bodyItem, member.Name];
                    if (value != null)
                    {
                        rowBody.Add(new XAttribute(member.Name, value));
                    }
                }

                rootBody.Add(rowBody);
            }

            return rootBody.ToString();
        }
        /// <summary>
        /// 读取xml返回信息里的错误内容
        /// </summary>
        /// <param name="err">错误信息</param>
        /// <param name="xmlResult">return的xml内容</param>
        /// <returns></returns>
        public static string GetXmlError(string err, XDocument xmlResult)
        {
            if (err == "" || err.Contains("详细错误如下"))
            {

                var nodezeroout = xmlResult.Descendants("zeroout").FirstOrDefault();
                if (nodezeroout != null)
                {
                    IEnumerable<XElement> xmlnodes = nodezeroout.Elements();

                    foreach (var xmlnode in xmlnodes)
                    {
                        if (xmlnode.Name.LocalName == "row")
                        {
                            string cinvcode = xmlnode.Attribute("cinvcode").Value;
                            string cwhname = xmlnode.Attribute("cwhname").Value;
                            string cbatch = xmlnode.Attribute("cbatch").Value;
                            string iquantity = xmlnode.Attribute("iquantity").Value;
                            string icurquantity = xmlnode.Attribute("icurquantity").Value;
                            string iforquantity = xmlnode.Attribute("iforquantity").Value;
                            string iavaquantity = xmlnode.Attribute("iavaquantity").Value;
                            string inum = xmlnode.Attribute("inum").Value;
                            string icurnum = xmlnode.Attribute("icurnum").Value;
                            string ifornum = xmlnode.Attribute("ifornum").Value;
                            string iavanum = xmlnode.Attribute("iavanum").Value;
                            err += cwhname+":"+ cinvcode + "：" + cbatch + "库存不足：现存量" + icurquantity + "，可用量" + iavaquantity + "，出库量" + iquantity + "，预计可用量" + iforquantity + "\r\n"
                                + "现存件数" + icurnum + "，可用件数" + iavanum + "，出库件数" + inum + "，预计可用件数" + ifornum;

                        }
                    }
                }

                var nodetoplow = xmlResult.Descendants("toplow").FirstOrDefault();
                if (nodetoplow != null)
                {
                    IEnumerable<XElement> xmlnodes = nodetoplow.Elements();
                    foreach (var xmlnode in xmlnodes)
                    {
                        if (xmlnode.Name.LocalName == "row")
                        {
                            string cinvcode = xmlnode.Attribute("cinvcode").Value;
                            err += cinvcode + "：有最高最低库存控制；" + "\r\n";
                        }
                    }
                }
            }

            return err;
        }


      
        /// <summary>
        /// U8生单结果解析
        /// </summary>
        /// <param name="cvouch">业务单据</param>
        /// <param name="badd">是否新增，0，1代表新增</param>
        /// <param name="err">返回错误</param>
        /// <param name="id">生单id</param>
        /// <param name="ccode">生单单据号</param>
        /// <param name="result">返回的xml</param>
        /// <returns></returns>
        public static bool GetCoResult(string cvouch, string badd, out string err, out string id, out string ccode, string result)
        {
            var xmlResult = XDocument.Parse(result); //实例化XmlDocument
            //取return 的信息、 如果add和verify一起，有两个return，
            //20241117取最后一个的，最后一个可能是错误的
            var nodeFirst = xmlResult.Descendants("return").FirstOrDefault();
            var nodeLast = xmlResult.Descendants("return").LastOrDefault();
            //if (CanShu.U8Recordlog == "true")
            //{
            //    WriteLog write = new WriteLog();
            //    write.WriteLogs(cvouch + ":" + result);
            //}

            if (nodeFirst == null)
            {
                //特殊情况，没有return的node
                var node2 = xmlResult.Descendants("returns").LastOrDefault();
                if (node2 != null)
                {
                    var resultmes2 = node2.Attribute("sErrors0").Value;
                    string strxml = "<" + resultmes2.Substring(resultmes2.IndexOf('<') + 1);

                    xmlResult = XDocument.Parse(strxml.Trim());
                    nodeFirst = xmlResult.Descendants("return").FirstOrDefault();
                    nodeLast = xmlResult.Descendants("return").LastOrDefault();
                    //依旧获取不到return的node
                    if (nodeFirst == null)
                    {
                        err = node2.ToString();
                        id = "";
                        ccode = "";
                        return false;
                    }
                }
                else
                {
                    err = XmlOperation.GetXmlError("", xmlResult);
                    id = "";
                    ccode = "";
                    return false;
                }
            }
            //判断是否成功，用 last
            var resultcode = nodeLast.Attribute("bRet").Value;
            var resultmes = nodeLast.Attribute("sError").Value;
            if (resultcode == "False")
            {
                err = resultmes;
                err = XmlOperation.GetXmlError(err, xmlResult);
                id = "";
                ccode = "";
                return false;
            }
            else
            {
                err = "";
                if (badd == "1")
                {
                    //取单据号，用first
                    //新增的时候，取
                    id = nodeFirst.Attribute("sRetID").Value;
                    ccode = nodeFirst.Attribute("sRetCode").Value;
                }
                else
                {
                    id = "";
                    ccode = "";
                }
                return true;
            }
        }

        /// <summary>
        /// 存货档案新增使用
        /// </summary>
        /// <param name="cvouch"></param>
        /// <param name="badd"></param>
        /// <param name="err"></param>
        /// <param name="id"></param>
        /// <param name="result"></param>
        /// <returns></returns>

        public static bool GetCoResult_as(string cvouch, string badd, out string err, out string id, string result)
        {
            var xmlResult = XDocument.Parse(result); //实例化XmlDocument
            //取return 的信息、 如果add和verify一起，有两个return，取第一个的
            var node = xmlResult.Descendants("return").FirstOrDefault();
            //if (CanShu.U8Recordlog == "true")
            //{
            //    WriteLog write = new WriteLog();
            //    write.WriteLogs(cvouch + ":" + result);
            //}

            if (node == null)
            {
                //特殊情况，没有return的node
                var node2 = xmlResult.Descendants("returns").FirstOrDefault();
                if (node2 != null)
                {
                    var resultmes2 = node2.Attribute("sErrors0").Value;
                    string strxml = "<" + resultmes2.Substring(resultmes2.IndexOf('<') + 1);

                    xmlResult = XDocument.Parse(strxml.Trim());
                    node = xmlResult.Descendants("return").FirstOrDefault();
                    //依旧获取不到return的node
                    if (node == null)
                    {
                        err = node2.ToString();
                        id = "";
                        return false;
                    }
                }
                else
                {
                    err = XmlOperation.GetXmlError("", xmlResult);
                    id = "";
                    return false;
                }
            }

            var resultcode = node.Attribute("bRet").Value;
            var resultmes = node.Attribute("sError").Value;
            if (resultcode == "False")
            {
                err = resultmes;
                err = XmlOperation.GetXmlError(err, xmlResult);
                id = "";
                return false;
            }
            else
            {
                err = "";
                if (badd == "1")
                {
                    //新增的时候，取
                    id = node.Attribute("sRetID").Value;
                }
                else
                {
                    id = "";
                }
                return true;
            }
        }
        /// <summary>
        /// U8生单结果解析,新增审核合并使用
        /// </summary>
        /// <param name="cvouch">业务单据</param>
        /// <param name="badd">是否新增，0，1代表新增</param>
        /// <param name="err">返回错误</param>
        /// <param name="id">生单id</param>
        /// <param name="ccode">生单单据号</param>
        /// <param name="result">返回的xml</param>
        /// <returns></returns>
        public static bool GetCoResult_addverify(string cvouch, string badd, out string err, out string id, out string ccode, string result)
        {
            var xmlResult = XDocument.Parse(result); //实例化XmlDocument
            //if (CanShu.U8Recordlog == "true")
            //{
            //    WriteLog write = new WriteLog();
            //    write.WriteLogs(cvouch + ":" + result);
            //}
            id = "";
            ccode = "";
            //取return 的信息、 如果add和verify一起，循环读取 
            IEnumerable<XElement> elements = xmlResult.XPathSelectElements("//return");//找根节点的直接book子节点
       
            string resultmes = "";
            Boolean berr = false;//是否有错误 
            foreach (XElement item in elements)
            {
                //Console.WriteLine(item.Name);
                if (item.Attribute("bRet").Value.ToLower() == "true")
                {
                    if (item.Attribute("OptType").Value == "add")
                    {
                        id = item.Attribute("sRetID").Value;
                        ccode = item.Attribute("sRetCode").Value;
                    }
                  
                }
                else
                {
                    berr = true;
                    resultmes = item.Attribute("sError").Value;
                }

            }
            if (berr)
            {
                if (string.IsNullOrWhiteSpace(resultmes) || resultmes.Contains("详细错误如下"))
                {
                    //有错误明细
                    err = resultmes;
                    err = XmlOperation.GetXmlError(err, xmlResult);
                    id = "";
                    ccode = "";
                    return false;
                }
                else
                {
                    //无错误明细
                    err = resultmes;
                    id = "";
                    ccode = "";
                    return false;
                }
            }
            else
            {
                err = "";
                return true;
            }
        }


            
            
       

        /// <summary>
        /// U8查询结果解析，查询客户档案
        /// </summary>
        /// <param name="cvouch">业务单据</param>
        /// <param name="badd">是否新增，0，1</param>
        /// <param name="err">返回错误</param>
        /// <param name="id">生单id</param>
        /// <param name="ccode">生单单据号</param>
        /// <param name="result">返回的xml</param>
        /// <returns></returns>
        public static bool GetCoQueryResult(string cvouch, string result,out string reContent )
        {
            var xmlResult = XDocument.Parse(result); //实例化XmlDocument
                                                     //取return 的信息、 如果add和verify一起，有两个return，取第一个的
            var node = xmlResult.Descendants("return").FirstOrDefault();
            //if (CanShu.U8Recordlog == "true")
            //{
            //    WriteLog write = new WriteLog();
            //    write.WriteLogs(cvouch + ":" + result);
            //}

            if (node != null)
            {
                var resultcode = node.Attribute("bRet").Value;
                var resultmes = node.Attribute("sError").Value;
                if (resultcode == "False")
                {
                    reContent = resultmes;
                    return false;
                }
                else
                {
                    var node2 = xmlResult.Descendants("customer").FirstOrDefault();
                    reContent = node2.ToString();
                    return true;
                }
               
            }
            else
            {
                reContent = "未执行完成";
                return false;
            } 
        }

         

        /// <summary>
        /// 获取指定账套标记
        /// </summary>
        /// <param name="cSys"></param>
        /// <returns></returns>
        public static string GetAccountConfigXML(string accid)
        {
            string cServerIP = Common.AppSettings.GetAppSeting("ServerIP");
            string cU8account = Common.AppSettings.GetAppSeting("U8Account");
            string cUsername = Common.AppSettings.GetAppSeting("UserName");
            string cUserpass = Common.AppSettings.GetAppSeting("UserPass");
            string xmlconfg = string.Format(@"<?xml version= '1.0'  encoding='utf-8'?>
<configuration>
  <appSettings>
    <add key='ServerIP' value='{0}'/>
    <add key='UserName' value='{1}'/>
    <add key='UserPass' value='{2}'/>
    <add key='U8Account' value='{3}'/>
    <add key='LoginDate' value='{4}'/>
    <add key='Lic' value='{5}'/>
  </appSettings>
</configuration>", cServerIP, cUsername, cUserpass, accid, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));

            return xmlconfg;
        }

        /// <summary>
        /// 获取指定账套标记,和当前单据日期
        /// </summary>
        /// <param name="cSys"></param>
        /// <returns></returns>
        public static string GetAccountConfigXML(string accid,string ddate)
        {
            string cServerIP = Common.AppSettings.GetAppSeting("ServerIP");
            string cU8account = Common.AppSettings.GetAppSeting("U8Account");
            string cUsername = Common.AppSettings.GetAppSeting("UserName");
            string cUserpass = Common.AppSettings.GetAppSeting("UserPass");
            string xmlconfg = string.Format(@"<?xml version= '1.0'  encoding='utf-8'?>
<configuration>
  <appSettings>
    <add key='ServerIP' value='{0}'/>
    <add key='UserName' value='{1}'/>
    <add key='UserPass' value='{2}'/>
    <add key='U8Account' value='{3}'/>
    <add key='LoginDate' value='{4}'/>
    <add key='Lic' value='{5}'/>
  </appSettings>
</configuration>", cServerIP, cUsername, cUserpass, accid, ddate, ddate);

            return xmlconfg;
        }
        /// <summary>
        /// 从配置文件获取U8数据源配置
        /// </summary>
        /// <returns></returns>
        public static string GetConfigXMLDate(string ddate)
        {
            string cServerIP = Common.AppSettings.GetAppSeting("ServerIP");
            string cU8account = Common.AppSettings.GetAppSeting("U8Account");
            string cUsername = Common.AppSettings.GetAppSeting("UserName");
            string cUserpass = Common.AppSettings.GetAppSeting("UserPass");
            string xmlconfg = string.Format(@"<?xml version= '1.0'  encoding='utf-8'?>
<configuration>
  <appSettings>
    <add key='ServerIP' value='{0}'/>
    <add key='UserName' value='{1}'/>
    <add key='UserPass' value='{2}'/>
    <add key='U8Account' value='{3}'/>
    <add key='LoginDate' value='{4}'/>
    <add key='Lic' value='{5}'/>
  </appSettings>
</configuration>", cServerIP, cUsername, cUserpass, cU8account, ddate, ddate);

            return xmlconfg;
        }

        /// <summary>
        /// 从配置文件获取U8数据源配置
        /// </summary>
        /// <returns></returns>
        public static string GetConfigXML()
        {
            string cServerIP = Common.AppSettings.GetAppSeting("ServerIP");
            string cU8account = Common.AppSettings.GetAppSeting("U8Account");
            string cUsername = Common.AppSettings.GetAppSeting("UserName");
            string cUserpass = Common.AppSettings.GetAppSeting("UserPass");
            string xmlconfg = string.Format(@"<?xml version= '1.0'  encoding='utf-8'?>
<configuration>
  <appSettings>
    <add key='ServerIP' value='{0}'/>
    <add key='UserName' value='{1}'/>
    <add key='UserPass' value='{2}'/>
    <add key='U8Account' value='{3}'/>
    <add key='LoginDate' value='{4}'/>
    <add key='Lic' value='{5}'/>
  </appSettings>
</configuration>", cServerIP, cUsername, cUserpass, cU8account, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));

            return xmlconfg;
        }

        /// <summary>
        /// 账套标记
        /// </summary>
        /// <param name="cSys"></param>
        /// <returns></returns>
        public static XDocument GetConfigXML(string cSys)
        {
            string cServerIP = ""; 
            string cU8account = "";
            string cUsername = "";
            string cUserpass = "";
            List<Orglist> objdata = Common.AppSettings.GetAppSetinglist("orglist");
            for (int i = 0; i < objdata.Count; i++)
            {
                if (objdata[i].Org == cSys)
                {
                    cServerIP = objdata[i].Server;
                    cU8account = objdata[i].U8Account;
                    cUsername= objdata[i].cUsername;
                    cUserpass = objdata[i].cPwd;
                    break;
                }
            
            }
            
            //2、配置api的xml 登陆字符串
           

            //构建数据库连接字符串           
            string cLoginDate = DateTime.Now.ToString("yyyy-MM-dd");
            System.Xml.Linq.XDocument xconstr = new XDocument();
            //声明不要
            XDeclaration xdec = new XDeclaration("1.0", "utf-8", "yes");
            xconstr.Declaration = xdec;
            XElement classConfig = new XElement("configuration");
            XElement classapp = new XElement("appSettings");
            XElement classadd = new XElement("add", new XAttribute("key", "ServerIP"), new XAttribute("value", cServerIP));
            classapp.Add(classadd);
            classadd = new XElement("add", new XAttribute("key", "UserName"), new XAttribute("value", cUsername));
            classapp.Add(classadd);
            classadd = new XElement("add", new XAttribute("key", "UserPass"), new XAttribute("value", cUserpass));
            classapp.Add(classadd);
            classadd = new XElement("add", new XAttribute("key", "U8Account"), new XAttribute("value", cU8account));
            classapp.Add(classadd);
            classadd = new XElement("add", new XAttribute("key", "LoginDate"), new XAttribute("value", cLoginDate));
            classapp.Add(classadd);
            classadd = new XElement("add", new XAttribute("key", "Lic"), new XAttribute("value", cLoginDate));
            classapp.Add(classadd);
            classConfig.Add(classapp);
            xconstr.Add(classConfig);
            return xconstr;
        }


        /// <summary>
        /// 账套标记，从数据库里读
        /// </summary>
        /// <param name="cSys"></param>
        /// <returns></returns>
        public static string GetConfigXMLFromDB(string cSys)
        {
            string cServerIP = "";
            string cU8account = "";
            string cUsername = "";
            string cUserpass = "";
            string sql = string.Format(@"select ServerIP,UserName,UserPass,U8Account from zdy_inter_database
where dbid ='{0}'", cSys);
            DataTable dt = DbHelper2.ExecuteTable(sql, CanShu.conStrInter);
            if (dt.Rows.Count == 0)
            {
                return "账套号未配置";
            }
            cServerIP =DbHelper.GetDbString(dt.Rows[0]["ServerIP"]);
            cU8account = DbHelper.GetDbString(dt.Rows[0]["U8Account"]);
            cUsername = DbHelper.GetDbString(dt.Rows[0]["UserName"]);
            cUserpass = DbHelper.GetDbString(dt.Rows[0]["UserPass"]);

            string  xmlconfg =string.Format(@"<?xml version= '1.0'  encoding='utf-8'?>
<configuration>
  <appSettings>
    <add key='ServerIP' value='{0}'/>
    <add key='UserName' value='{1}'/>
    <add key='UserPass' value='{2}'/>
    <add key='U8Account' value='{3}'/>
    <add key='LoginDate' value='{4}'/>
    <add key='Lic' value='{5}'/>
  </appSettings>
</configuration>", cServerIP, cUsername, cUserpass, cU8account,DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));

            return xmlconfg;
            //2、配置api的xml 登陆字符串


            //构建数据库连接字符串           
            //string cLoginDate = DateTime.Now.ToString("yyyy-MM-dd");
            //System.Xml.Linq.XDocument xconstr = new XDocument();
            //声明不要
            //XDeclaration xdec = new XDeclaration("1.0", "utf-8", "yes");
            //xconstr.Declaration = xdec;
            //XElement classConfig = new XElement("configuration");
            //XElement classapp = new XElement("appSettings");
            //XElement classadd = new XElement("add", new XAttribute("key", "ServerIP"), new XAttribute("value", cServerIP));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "UserName"), new XAttribute("value", cUsername));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "UserPass"), new XAttribute("value", cUserpass));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "U8Account"), new XAttribute("value", cU8account));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "LoginDate"), new XAttribute("value", cLoginDate));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "Lic"), new XAttribute("value", cLoginDate));
            //classapp.Add(classadd);
            //classConfig.Add(classapp);
            //xconstr.Add(classConfig);
            //return xconstr;
        }

        /// <summary>
        /// 账套标记，根据mes的账套号进行更改，从数据库里读
        /// </summary>
        /// <param name="cSys"></param>
        /// <returns></returns>
        public static string GetConfigXMLFromDBIMS(string cSys)
        {
            string cServerIP = "";
            string cU8account = "";
            string cUsername = "";
            string cUserpass = "";
            string sql = string.Format(@"select ServerIP,UserName,UserPass,U8Account from zdy_inter_database
where MESORG ='{0}'", cSys);
            DataTable dt = DbHelper2.ExecuteTable(sql, CanShu.conStrInter);
            if (dt.Rows.Count == 0)
            {
                return "账套号未配置";
            }
            cServerIP = DbHelper.GetDbString(dt.Rows[0]["ServerIP"]);
            cU8account = DbHelper.GetDbString(dt.Rows[0]["U8Account"]);
            cUsername = DbHelper.GetDbString(dt.Rows[0]["UserName"]);
            cUserpass = DbHelper.GetDbString(dt.Rows[0]["UserPass"]);

            string xmlconfg = string.Format(@"<?xml version= '1.0'  encoding='utf-8'?>
<configuration>
  <appSettings>
    <add key='ServerIP' value='{0}'/>
    <add key='UserName' value='{1}'/>
    <add key='UserPass' value='{2}'/>
    <add key='U8Account' value='{3}'/>
    <add key='LoginDate' value='{4}'/>
    <add key='Lic' value='{5}'/>
  </appSettings>
</configuration>", cServerIP, cUsername, cUserpass, cU8account, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));

            return xmlconfg;
            //2、配置api的xml 登陆字符串


            //构建数据库连接字符串           
            //string cLoginDate = DateTime.Now.ToString("yyyy-MM-dd");
            //System.Xml.Linq.XDocument xconstr = new XDocument();
            //声明不要
            //XDeclaration xdec = new XDeclaration("1.0", "utf-8", "yes");
            //xconstr.Declaration = xdec;
            //XElement classConfig = new XElement("configuration");
            //XElement classapp = new XElement("appSettings");
            //XElement classadd = new XElement("add", new XAttribute("key", "ServerIP"), new XAttribute("value", cServerIP));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "UserName"), new XAttribute("value", cUsername));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "UserPass"), new XAttribute("value", cUserpass));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "U8Account"), new XAttribute("value", cU8account));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "LoginDate"), new XAttribute("value", cLoginDate));
            //classapp.Add(classadd);
            //classadd = new XElement("add", new XAttribute("key", "Lic"), new XAttribute("value", cLoginDate));
            //classapp.Add(classadd);
            //classConfig.Add(classapp);
            //xconstr.Add(classConfig);
            //return xconstr;
        }


        /// <summary>
        /// 类转xml ，序列化，不包含xml声明
        /// </summary>
        /// <param name="Obj"></param>
        /// <returns></returns>
        public static string ObjectToXmlSerializer(Object Obj)
        {
            // 先清理空字符串字段为 null
            //CleanEmptyProperties(Obj);

            XmlWriterSettings settings = new XmlWriterSettings();
            //去除xml声明
            settings.OmitXmlDeclaration = true;
            settings.Encoding = Encoding.Default;
            System.IO.MemoryStream mem = new MemoryStream();
            using (XmlWriter writer = XmlWriter.Create(mem, settings))
            {
                //去除默认命名空间xmlns:xsd和xmlns:xsi
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                XmlSerializer formatter = new XmlSerializer(Obj.GetType());
                formatter.Serialize(writer, Obj, ns);
            }
            return Encoding.Default.GetString(mem.ToArray());
        }

        // 先清理空字符串字段为 null
        public static void CleanEmptyProperties(object obj)
        {
            if (obj == null) return;

            foreach (var prop in obj.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(string))
                {
                    var value = (string)prop.GetValue(obj);
                    if (value != null && value.Trim() == "")
                    {
                        prop.SetValue(obj, null); // 设置为 null 后，ShouldSerialize 就会生效
                    }
                }
            }
        }
        /// <summary>
        /// XML转类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T DESerializer<T>(string strXML) where T : class
        {
            try
            {
                using (StringReader sr = new StringReader(strXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
